package com.cqc.jdk8.Jutil.concurrent.threadPoolExecutor.exception;


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author cui
 * @date 2023/11/10 21:19
 * @description
 **/
public class ProcessExceptionThreadPoolExecutor extends ThreadPoolExecutor {


    public ProcessExceptionThreadPoolExecutor(
            int corePoolSize, int maximumPoolSize,
            long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        //execute()发生了异常
        if (null != t) {
            t.printStackTrace();
        }

        if (r instanceof FutureTask) {
            try {
                //submit()发生了异常，可以通过get获取异常
                FutureTask futureTask = (FutureTask) r;
                Object o = futureTask.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
